package offer;

public class Singleton {
    private String value;

    private static Singleton instance;
    /**
     * 设计一个类，只能生成该类的一个实例
     * @param args
     */
    public static void main(String[] args) {
        Singleton sig = getSingleton();
        sig.setValue("12");
        System.out.println(sig.value);
        Singleton sig1 = getInstance_1();

        System.out.println(sig1.value);
    }

    private Singleton(){}

    private static class SingletonHolder{
        private static final Singleton singleton = new Singleton();
    }

    public static Singleton getSingleton() {
        return SingletonHolder.singleton;
    }

    public void setValue(String value) {
        this.value  = value;
    }

    public static Singleton getInstance_1() {
        if (instance==null){
            synchronized (Singleton.class){
                if (instance==null){
                    instance = new Singleton();
                    instance.setValue("from sync");
                }
            }
        }
        return instance;
    }
}
